function [fb_arr,pb_arr,snips,fb_pert_num,pb_pert_num]=meta_receptive_field_map(proj_meta,sites)

color_array=[];
for ind=1:12
    color_array(13-ind,:)=[0 0 0]+(ind*(1/12));
end

fb_arr=[];
pb_arr=[];
% sites=[2 4];
ini_cnt=1;
snips={};
fb_pert_num=[];
pb_pert_num=[];
for knd=1:length(sites)
    cur_site=sites(knd);
    all_act={};
    pl_cells=[];
    for tnd=1:size(proj_meta(cur_site).rd,2)
        cnt=0;
        for vnd=1:size(proj_meta(cur_site).rd,1)
            all_act{tnd}(cnt+1:cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1),:)=proj_meta(cur_site).rd(vnd,tnd).act;
            cnt=cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1);
            pl_cells(vnd)=cnt;
        end
    end
    fb_snip={};
    pb_snip={};
    fb_av={};
    pb_av={};
    for tnd=1:size(proj_meta(cur_site).rd,2)
        nbr_frames=proj_meta(cur_site).rd(1,tnd).nbr_frames;
        stim_id=proj_meta(cur_site).rd(1,tnd).ps_id;
        runP=proj_meta(cur_site).rd(1,tnd).velP_smoothed>0.005;
        runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.005;
        runM_raw=proj_meta(cur_site).rd(1,tnd).velM_smoothed;
        for lnd=1:size(all_act{1},1)
            act=all_act{tnd}(lnd,:);
            [fb_av{lnd,tnd},pb_av{lnd,tnd},fb_snip{lnd,tnd},pb_snip{lnd,tnd}]=pert_ana_meta(nbr_frames,stim_id,runM,runP,runM_raw,act);
        end
    end
    for pnd=1:12
        for tnd=1:size(proj_meta(cur_site).rd,2)
            fb_pert_num(tnd,pnd,knd)=size(fb_snip{1,tnd}{pnd},2);
            pb_pert_num(tnd,pnd,knd)=size(pb_snip{1,tnd}{pnd},2);
            cnt=ini_cnt;
            for lnd=1:size(all_act{1},1)
                if ~isempty(fb_snip{lnd,tnd}{pnd})
                    cur_trace=fb_snip{lnd,tnd}{pnd};
                    fb_arr(cnt,:,tnd,pnd)=mean(cur_trace,2)';
                    if size(cur_trace,2)>2
                        t_arr_fb(cnt,pnd,tnd)=ttest(mean(cur_trace(13:20,:),1),mean(cur_trace(1:8,:),1),0.0042,'right');
                    else
                        t_arr_fb(cnt,pnd,tnd)=NaN;
                    end
                else
                    fb_arr(cnt,:,tnd,pnd)=zeros(1,26);
                    t_arr_fb(cnt,pnd,tnd)=NaN;
                end
                if ~isempty(pb_snip{lnd,tnd}{pnd})
                    cur_trace=pb_snip{lnd,tnd}{pnd};
                    pb_arr(cnt,:,tnd,pnd)=mean(cur_trace,2)';
                    if size(cur_trace,2)>2
                        t_arr_pb(cnt,pnd,tnd)=ttest(mean(cur_trace(13:20,:),1),mean(cur_trace(1:8,:),1),0.0042,'right');
                    else
                        t_arr_pb(cnt,pnd,tnd)=NaN;
                    end
                else
                    pb_arr(cnt,:,tnd,pnd)=zeros(1,26);
                    t_arr_pb(cnt,pnd,tnd)=NaN;
                end
                cnt=cnt+1;
            end
        end
    end
    snips{knd,1}=fb_snip;
    snips{knd,2}=pb_snip;
    ini_cnt=ini_cnt+size(all_act{1},1);
end

neuron_num=[];
for knd=1:2
    cur_site=sites(knd);
    for tnd=1:size(proj_meta(cur_site).rd,1)
        neuron_num(tnd,knd)=size(proj_meta(cur_site).rd(tnd,1).act,1);
    end
end

% for pnd=1:12
%     figure;
% %     set(gcf,'pos',[2060         581        1659         353])
%     for tnd=1:7
%         subplot(1,7,tnd)
%         line([7,7],[0.99 1.15],'color','k','linewidth',2)
%         hold on
%         cur_fb=mean(fb_arr(1:574,:,tnd,pnd),1);
%         cur_fb=cur_fb+diff([cur_fb(6) 1]);
%         cur_pb=mean(pb_arr(1:574,:,tnd,pnd),1);
%         cur_pb=cur_pb+diff([cur_pb(6) 1]);
%         plot(cur_fb,'k','linewidth',2)
%         plot(cur_pb,'g','linewidth',2)
%         ylim([0.98 1.15])
%     end
% end

% site_ini=1;
% site_last=976;
% sequence=[1 2 3 7 8 9 4 5 6 10 11 12];
% figure;
% for pnd=1:12
%     set(gcf,'pos',[35   417   848   380])
%     for tnd=1:1
%         subplot(2,6,sequence(pnd))
%         rectangle('position',[11 0.97 10 1.15],'facecolor',[0.9 0.9 0.9],'linestyle','none')
% %         line([6,6],[0.99 1.15],'color','k','linewidth',2)
%         hold on
%         cur_fb=mean(fb_arr(site_ini:site_last,:,tnd,pnd),1);
%         cur_fb=cur_fb+diff([cur_fb(11) 1]);
%         cur_pb=mean(pb_arr(site_ini:site_last,:,tnd,pnd),1);
%         cur_pb=cur_pb+diff([cur_pb(11) 1]);
%         plot(cur_fb,'k','linewidth',2)
%         plot(cur_pb,'g','linewidth',2)
%         ylim([0.97 1.15])
%     end
% end

% site_ini=575;
% site_last=976;
% pop_lfm=[];
% for pnd=1:12
%     for tnd=1:7
%         cur_fb=fb_arr(site_ini:site_last,:,tnd,pnd);
%         cur_fb(sum(cur_fb,2)==0,:)=[];
%         cur_fb=mean(cur_fb,1);
%         pop_lfm(tnd,1,pnd)=mean(cur_fb(11:15))-mean(cur_fb(1:5));
%         cur_pb=pb_arr(site_ini:site_last,:,tnd,pnd);
%         cur_pb(sum(cur_pb,2)==0,:)=[];
%         cur_pb=mean(cur_pb,1);
%         pop_lfm(tnd,2,pnd)=mean(cur_pb(11:15))-mean(cur_pb(1:5));
%         cur_full=mean([cur_fb; cur_pb]);
%         pop_lfm(tnd,3,pnd)=mean(cur_full(11:15))-mean(cur_full(1:5));
%     end
% end
% sequence=[1 2 3 7 8 9 4 5 6 10 11 12];
% figure;
% % set(gcf,'pos',[2060         581        1659         353])
% for pnd=1:12
%     subplot(2,6,sequence(pnd))
%     plot(pop_lfm(:,1,pnd),'k')
%     hold on
%     plot(pop_lfm(:,2,pnd),'g')
% %     plot(pop_lfm(:,3,pnd),'color','b','linewidth',2)
% %     ylim([min(pop_lfm(:)) max(pop_lfm(:))])
%     ylim([min(pop_lfm(:)) max(pop_lfm(:))])
%     xlim([0 7])
% end

% pop_mean_fluo=[];
% for pnd=1:12
%     for tnd=1:7
%         cur_fb=fb_arr(:,:,tnd,pnd);
%         cur_fb(sum(cur_fb,2)==0,:)=[];
%         cur_fb=mean(cur_fb,1);
%         pop_mean_fluo(tnd,1,pnd)=mean(cur_fb(11:15));
%         pop_mean_fluo(tnd,4,pnd)=mean(cur_fb(1:5));
%         cur_pb=pb_arr(:,:,tnd,pnd);
%         cur_pb(sum(cur_pb,2)==0,:)=[];
%         cur_pb=mean(cur_pb,1);
%         pop_mean_fluo(tnd,2,pnd)=mean(cur_pb(11:15));
%         pop_mean_fluo(tnd,5,pnd)=mean(cur_pb(1:5));
%         cur_full=mean([cur_fb; cur_pb]);
%         pop_mean_fluo(tnd,3,pnd)=mean(cur_full(11:15));
%     end
% end
% figure;
% % set(gcf,'pos',[2060         581        1659         353])
% for pnd=1:12
%     subplot(2,6,sequence(pnd))
%     plot(pop_mean_fluo(:,1,pnd),'k')
%     hold on
%     plot(pop_mean_fluo(:,4,pnd),'--k')
%     plot(pop_mean_fluo(:,2,pnd),'g')
%     plot(pop_mean_fluo(:,5,pnd),'--g')
% %     plot(pop_mean_fluo(:,3,pnd),'color','b','linewidth',2)
% %     ylim([min(pop_lfm(:)) max(pop_lfm(:))])
%     ylim([min(pop_mean_fluo(:)) max(pop_mean_fluo(:))])
% end





% roi_ids={};
% cnt=1;
% for knd=1:2
%     cur_site=sites(knd);
%     for lnd=1:4
%         for gnd=1:size(proj_meta(cur_site).rd(lnd,1).act,1)
%             roi_ids{cnt}=proj_meta(cur_site).rd(lnd,1).ROIinfo(gnd).indices;
%             cnt=cnt+1;
%         end
%     end
% end
% 
% % figure;
% % for pnd=1:12
% %     for tnd=1:7
% %         for lnd=1:976
% %             ids=find(t_arr_pb(lnd,pnd,tnd)==1);
% %             for gnd=1:length(ids)
% %                 hold on
% %                 plot(pb_arr(lnd,:,tnd,pnd))
% %             end
% %         end
% %     end
% % end
% 
% figure;
% rectangle('position',[0 1 1 1],'facecolor',color_array(1,:))
% hold on
% rectangle('position',[1 1 1 1],'facecolor',color_array(2,:))
% rectangle('position',[2 1 1 1],'facecolor',color_array(3,:))
% rectangle('position',[0 0 1 1],'facecolor',color_array(4,:))
% rectangle('position',[1 0 1 1],'facecolor',color_array(5,:))
% rectangle('position',[2 0 1 1],'facecolor',color_array(6,:))
% rectangle('position',[3 1 1 1],'facecolor',color_array(7,:))
% rectangle('position',[4 1 1 1],'facecolor',color_array(8,:))
% rectangle('position',[5 1 1 1],'facecolor',color_array(9,:))
% rectangle('position',[3 0 1 1],'facecolor',color_array(10,:))
% rectangle('position',[4 0 1 1],'facecolor',color_array(11,:))
% rectangle('position',[5 0 1 1],'facecolor',color_array(12,:))
% 
% figure('color',[0 0 0]);
% for gnd=1:12
%     hold on
%     plot(fb_pert_num(:,gnd,1),'color',color_array(gnd,:))
% end
% xlim([1 10])
% legend('1','2','3','4','5','6','7','8','9','10','11','12')
% box off
% axis off
% 
% figure;
% template=zeros(400,750);
% weird=[];
% non_resp=[];
% for gnd=1:82
%     cur_lfm=logical(nansum([t_arr_fb(gnd,:,1); t_arr_pb(gnd,:,1)]));
%     ids=find(cur_lfm);
%     if length(ids)==1
%         template(roi_ids{gnd})=color_array(ids,1);
%     elseif length(ids)==2
%         len=length(roi_ids{gnd});
%         template(roi_ids{gnd}(1:round(len/2)))=color_array(ids(1),1);
%         template(roi_ids{gnd}(round(len/2)+1:end))=color_array(ids(2),1);
%     elseif length(ids)>2
%         weird(gnd)=gnd;
%     else
%         non_resp(gnd)=gnd;
%     end
% end
% weird(weird==0)=[];
% non_resp(non_resp==0)=[];